package com.qf.blog.service.impl;


import com.qf.blog.commons.constant.RedisKeys;
import com.qf.blog.entity.Invitation;
import com.qf.blog.entity.User;
import com.qf.blog.service.CommentService;

import com.qf.blog.service.IInvitationService;
import com.qf.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class PostScoreService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IInvitationService invitationService;

    @Autowired
    private CommentService commentService;

    @Autowired
    @Lazy
    private LikeService likeService;

    @Autowired
    private UserService userService;

    public void addPostIdToRedis(Integer postId) {
        stringRedisTemplate.opsForSet().add(RedisKeys.POST_SCORE_KEY, postId.toString());
    }

    /**
     * 实现分数的计算
     *
     * @param postId
     */
    public void computeScore(Integer postId) {

        // 1. 根据帖子Id查询帖子信息
        Invitation post = invitationService.getById(postId);

        // 2.获取它的姿态(加精)
        Integer status = post.getStatus();

        // 3.获取帖子的点赞数量
        Long likeCount = likeService.getLikeCount(1, postId);

        // 4.查询帖子下面的评论数量
//        Long commentCount = commentService.getCommentCountByPostId(postId);

        // 5.查询帖子的作者
        User user = userService.getById(post.getUserId());

        // 5.计算分数
        Double score = 10.33333;

        // 6.更新帖子的分数
//        invitationService.updatePostSorce(postId,score);

    }


    public static void main(String[] args) {

        Integer status = 0; // 帖子是否加精
        Long likeCount = 100L;  // 点赞数量
        Long commentCount = 200L; //  评论数量
        Integer userIdLevel = 3; // 作者的权重

        int weight = status == 1 ? 100 : 0; // 如果加精了，多个100

        Long count = (likeCount * 100) + (commentCount * 200) + weight + (userIdLevel * 50);

        // 根据总数量计算出一个double值,计算出一个数值对数
        double postScore = Math.log(count);

        System.out.println(postScore);

    }
}
